import {SET_TRIP_ALL,CLEAR_TRIP,SET_RECENT_TRIP,DELETE_TRIP} from '../actions/healthAction'

let healthState = {
    
    phone:"",
    city:"",
    area:"",
    address:"",
    personType:"stayLong",
    stayType:"home",
    symptomType:["fever"],
    symptom:"",
    temperature:"no",
    recentTrip:[]
}

let TYPES = {
    // 缓存数据
    [SET_TRIP_ALL](state,actions){
        const {phone,city,area,address,personType,stayType,symptomType,symptom,temperature,recentTrip} = actions.value
        state.phone = phone
        state.city = city
        state.area = area
        state.address = address
        state.personType = personType
        state.stayType = stayType
        state.symptomType = [...symptomType]
        state.symptom = symptom
        state.temperature = temperature
        state.recentTrip = [...recentTrip]
    },
    // 清空缓存
    [CLEAR_TRIP](state,actions){
        state.phone = ""
        state.city  = ""
        state.area = ""
        state.address = ""
        state.personType = "stayLong"
        state.stayType = "home"
        state.symptomType = ["fever"]
        state.symptom = ""
        state.temperature = "no"
        state.recentTrip = []
    },
    // 添加/编辑行程
    [SET_RECENT_TRIP](state,actions){
        let index = state.recentTrip.findIndex(item=>item.tripId === actions.value.tripId)
        // 查找当前元素 
        if(index!==-1){
            // 找到替换
            state.recentTrip[index] = {...actions.value} 
            return 
        }
        // 找不到新增
        state.recentTrip.push(actions.value)

        // 数组更新
        state.recentTrip = [...state.recentTrip]
    },
    
    // 删除行程
    [DELETE_TRIP](state,actions){
        // 根据id查找
        let index = state.recentTrip.findIndex(item=>item.tripId === actions.id)
        // 删除
        state.recentTrip.splice(index,1)
        // 数组更新
        state.recentTrip = [...state.recentTrip]
    }
}

let health = (state = healthState,actions) =>{
  
    // 在运行过程中 行为不同 我的算法会被动态改变
    TYPES[actions.type]&&TYPES[actions.type](state,actions)

    return {...state}    
}

export default health